YZYZ.Admin							= {};
YZYZ.Admin.RouteGroup 				= {};
YZYZ.Admin.RouteGroup.grid 			= null;
YZYZ.Admin.RouteGroup.store			= null;
YZYZ.Admin.RouteGroup.columnModel 	= null;
YZYZ.Admin.RouteGroup.newWin 		= null;
YZYZ.Admin.RouteGroup.newForm 		= null;
YZYZ.Admin.RouteGroup.editWin 		= null;
YZYZ.Admin.RouteGroup.editForm 		= null;

YZYZ.Admin.RouteGroup.newRecord = Ext.data.Record.create([
	{ name: 'id', type: 'int'},
	{ name: 'name', type: 'string'}
]);
YZYZ.Admin.RouteGroup.submitAdd=function(){
	var name = Ext.getCmp("addName").getValue();
	if(Ext.isEmpty(name)){
		YZYZ.Curtain.alert("名称不能为空");
		Ext.getCmp("addName").markInvalid();
		return;
	}
	if(name.length > 20){
		YZYZ.Curtain.alert("名称长度不能超过20");
		Ext.getCmp("addName").markInvalid();
		return;
	}
	YZYZ.Curtain.msg("系统处理中....");
	Ext.Ajax.request( {
		url		: Ext.get("addRouteGroupUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			name	: name
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("添加成功");
				var rec = new YZYZ.Admin.RouteGroup.newRecord({
					name	: name
				});
				rec.set("id", result.id);
				YZYZ.Admin.RouteGroup.grid.getStore().insert(0, rec);
				YZYZ.Admin.RouteGroup.newWin.hide();
			}
			else {
				YZYZ.Curtain.alert(result.message);
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("程序出现错误或网络故障");
		}
	});
};
YZYZ.Admin.RouteGroup.submitEdit=function(){
	var name = Ext.getCmp("editName").getValue();
	if(Ext.isEmpty(name)){
		YZYZ.Curtain.alert("名称不能为空");
		Ext.getCmp("editName").markInvalid();
		return;
	}
	if(name.length > 20){
		YZYZ.Curtain.alert("名称长度不能超过20");
		Ext.getCmp("editName").markInvalid();
		return;
	}
	var rec = YZYZ.Admin.RouteGroup.grid.getSelectionModel().getSelected();
	YZYZ.Curtain.msg("系统处理中....");
	Ext.Ajax.request( {
		url		: Ext.get("editRouteGroupUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			name	: name,
			groupId : rec.get("id")
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("修改成功");
				YZYZ.Admin.RouteGroup.grid.getSelectionModel().getSelected().set("name",name);
				YZYZ.Admin.RouteGroup.grid.getStore().commitChanges();
				YZYZ.Admin.RouteGroup.editWin.hide();
			}
			else {
				YZYZ.Curtain.alert(result.message);
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("程序出现错误或网络故障");
		}
	});	
};
YZYZ.Admin.RouteGroup.showEditWin=function(){
	if(!YZYZ.Admin.RouteGroup.editWin){
		YZYZ.Admin.RouteGroup.editForm = new Ext.form.FormPanel({
			width:450,
			height:200,
			region:'center',
			bodyStyle:"padding-top:40px;",
			labelAlign:'right',
			items:[{
				xtype:'textfield',
				width:300,
				maxLength:20,
				id:'editName',
				fieldLabel:'名称'
			}]
		});
		YZYZ.Admin.RouteGroup.editWin = new Ext.Window({
			title:'编辑板块',
			width:450,
			height:200,
			closable:true,
			layout:'border',
			closeAction:'hide',
			modal:true,
			items:[YZYZ.Admin.RouteGroup.editForm],
			buttons:[{
				text:'提交',
				width:60,
				handler:YZYZ.Admin.RouteGroup.submitEdit
			},{
				text:'关闭',
				width:60,
				handler:function(){YZYZ.Admin.RouteGroup.editWin.hide();}
			}]
		});
	}
	YZYZ.Admin.RouteGroup.editWin.show();
	var rec = YZYZ.Admin.RouteGroup.grid.getSelectionModel().getSelected();
	Ext.getCmp("editName").setValue(rec.get("name"));
};

YZYZ.Admin.RouteGroup.showNewWin=function(){
	if(!YZYZ.Admin.RouteGroup.newWin){
		YZYZ.Admin.RouteGroup.newForm = new Ext.form.FormPanel({
			width:450,
			height:200,
			region:'center',
			bodyStyle:"padding-top:40px;",
			labelAlign:'right',
			modal:true,
			items:[{
				xtype:'textfield',
				width:300,
				maxLength:20,
				id:'addName',
				fieldLabel:'名称'
			}]
		});
		YZYZ.Admin.RouteGroup.newWin = new Ext.Window({
			title:'添加新板块',
			width:450,
			height:200,
			closable:true,
			layout:'border',
			closeAction:'hide',
			items:[YZYZ.Admin.RouteGroup.newForm],
			buttons:[{
				text:'提交',
				width:60,
				handler:YZYZ.Admin.RouteGroup.submitAdd
			},{
				text:'关闭',
				width:60,
				handler:function(){YZYZ.Admin.RouteGroup.newWin.hide();}
			}]
		});
	}
	
	YZYZ.Admin.RouteGroup.newWin.show();
	Ext.getCmp("addName").setValue("");
};

YZYZ.Admin.RouteGroup.opRender=function(value, metaData, record, rowIndex, colIndex, store){
	var html = "<div class='btnCotainer'>";
	html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.RouteGroup.showEditWin()' class='btnEdit'>编辑</button></div>";
	html = html + "<div class='divBtn'><button type='button' class='btnEdit' onclick='YZYZ.Admin.RouteGroup.confirm("+record.get('id')+")'>删除</button></div>";
	html = html + "</div>";
	return html;
};

YZYZ.Admin.RouteGroup.init=function(){
	 YZYZ.Admin.RouteGroup.columnModel=new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
            {header: '名称', width: 300, sortable: false, hideable:false, dataIndex: 'name'},
            {header: '操作',id:'operations', width: 250, sortable: false, hideable:false, dataIndex: 'id', renderer:YZYZ.Admin.RouteGroup.opRender}
    	]);
    YZYZ.Admin.RouteGroup.store= new Ext.data.Store({
			proxy: new Ext.data.HttpProxy ({
	            url: document.getElementById("getRouteGroupUrl").value
	        }),
	        reader: new Ext.data.JsonReader({
	        	root			: "data",
	        	totalProperty	: "sum"
        	}, [
                {name:'id'},
                {name:'name'}
            ]),
	        remoteSort: false
		});
	YZYZ.Admin.RouteGroup.grid = new Ext.grid.GridPanel({
			id			: "routeGroupGrid",
			store		: YZYZ.Admin.RouteGroup.store,
			cm			: YZYZ.Admin.RouteGroup.columnModel,
			sm			: new Ext.grid.RowSelectionModel(),
			region		: 'center',
			height		: Ext.getBody().getHeight()-100,
			width		: Ext.getBody().getWidth(),
			viewConfig	: { forceFit:true },
			autoExpandColumn : 'operations',
			loadMask	: { msg:"数据加载中....." },
			bbar		: new Ext.PagingToolbar({   
				pageSize	: 30,   
				store		: YZYZ.Admin.RouteGroup.store,   
				displayInfo	: true,   
				displayMsg	: "第{0}条到{1}条  共{2}条",   
				emptyMsg	: "没有行程板块",
				items		:[{text:'添加行程板块',handler:YZYZ.Admin.RouteGroup.showNewWin}]
			})              
		});	
	new Ext.Viewport({
		layout: 'border',
		items:[{
			region			: 'center',
			cls				: 'divGrid',
			items 			: [YZYZ.Admin.RouteGroup.grid],
			autoScroll		: false,
			layout			: 'fit' 
		}]
	});
	YZYZ.Admin.RouteGroup.grid.getStore().load({params :{start:0,limit:30}});
};
Ext.onReady(YZYZ.Admin.RouteGroup.init,document);